<?php

namespace app\api\library\user\getUserTradeInfo;

use app\api\model\MallUser;

use think\facade\Db;

/**
 *
 * User: chenzheng
 * Date: 2023/3/22
 * Time: 16:42
 */
class V
{
    public static function run($param)
    {
        
        $userId = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $date   = $param[ 'date' ];
        $type   = $param[ 'type' ];
        $where  = [
            ['id', '=', $userId],
        ];
        
        $userInfo = MallUser::where($where)->find();
        $page     = $param[ 'page' ];
        $limit    = $param[ 'limit' ];
        $offset   = $limit * ($page - 1);
        if (empty($userInfo)) {
            _outPut(204, '', '用户不存在');
        }
        $where = [
            ['user_id', '=', $userId],
            ['delete_time', '=', null],
        ];
        
        
        //查询在提现中的数据
        
        $sql = "select group_concat(distinct a.id) ids from ea_mall_trade_record_log a left join ea_mall_withdraw_record b on a.order_id = b.id where a.trade_type in (14,15,16) and b.status in (0,1,10) and a.user_id = " . $userId;
        // var_dump($sql);die;
        $mx = Db::query($sql)[ 0 ][ 'ids' ];
        // var_dump($mx);die;
        if ($mx) {
            $where[] = ['id', 'not in', $mx];
        }
        if ($type > 0) {
            $where[] = ['trade_type', '=', $type];
        }
        if ( !empty($date)) {
            $startTime = strtotime($date . '-1');
            $endTime   = strtotime(date($date . '-t 23:59:59'));
            $where[]   = ['create_time', '>', $startTime];
            $where[]   = ['create_time', '<', $endTime];
        }
        $total       = Db::name('mall_trade_record_log')
            ->where($where)
            ->count();
        $tradeRecord = Db::name('mall_trade_record_log')
            ->where($where)
            ->limit($offset, $limit)
            ->field('id,trade_type,amount,balance,order_id,create_time')
            ->order(['id' => 'desc'])
            ->select()->toArray();
        $orderIds    = array_column($tradeRecord, 'order_id');
        $orders      = Db::view('mall_order', 'id,collage_id')
            ->view('mall_collage', 'collage_id,collage_no,start_time,end_time', 'mall_order.collage_id=mall_collage.collage_id')
            ->where([['mall_order.id', 'in', $orderIds]])
            ->select();
        $orders      = $orders->toArray();
        $orders      = array_column($orders, null, 'id');
        foreach ($tradeRecord as $key => $value) {
            switch ($value[ 'trade_type' ]) {
                case 1:
                    $tradeRecord[ $key ][ 'trade' ] = '订单支付（微信）';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 2:
                    $tradeRecord[ $key ][ 'trade' ] = '订单支付（余额）';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 3:
                    $tradeRecord[ $key ][ 'trade' ] = '团购邀请奖励';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 4:
                    $tradeRecord[ $key ][ 'trade' ] = '成团奖励退回';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 6:
                    $tradeRecord[ $key ][ 'trade' ] = '提现被驳回';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 7:
                    $tradeRecord[ $key ][ 'trade' ] = '团购成团奖励';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 10:
                    $tradeRecord[ $key ][ 'trade' ] = '订单退款（微信）';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 11:
                    $tradeRecord[ $key ][ 'trade' ] = '订单退款（余额）';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 12:
                    $tradeRecord[ $key ][ 'trade' ] = '团购失败退款（微信）';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 13:
                    $tradeRecord[ $key ][ 'trade' ] = '团购失败退款（余额）';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                case 14:
                    $tradeRecord[ $key ][ 'trade' ] = '微信收款码提现';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 15:
                    $tradeRecord[ $key ][ 'trade' ] = '支付宝收款码提现';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 16:
                    $tradeRecord[ $key ][ 'trade' ] = '支付宝账号提现';
                    $tradeRecord[ $key ][ 'type' ]  = 0;
                    break;
                case 17:
                    $tradeRecord[ $key ][ 'trade' ] = '推广奖励';
                    $tradeRecord[ $key ][ 'type' ]  = 1;
                    break;
                default:
                    $tradeRecord[ $key ][ 'trade' ] = '';
            }
            $tradeRecord[ $key ][ 'create_time' ] = date('Y-m-d H:i:s', $value[ 'create_time' ]);
            $tradeRecord[ $key ][ 'amount' ]      = getPrice($value[ 'amount' ]);
            $tradeRecord[ $key ][ 'collage_no' ]  = '';
            $tradeRecord[ $key ][ 'start_time' ]  = '';
            $tradeRecord[ $key ][ 'end_time' ]    = '';
            if (isset($orders[ $value[ 'order_id' ] ])) {
                $tradeRecord[ $key ][ 'collage_no' ] = $orders[ $value[ 'order_id' ] ][ 'collage_no' ];
                $tradeRecord[ $key ][ 'start_time' ] = $orders[ $value[ 'order_id' ] ][ 'start_time' ];
                $tradeRecord[ $key ][ 'end_time' ]   = $orders[ $value[ 'order_id' ] ][ 'end_time' ];
            }
        }
        $page          = [
            'total'      => $total,
            'limit'      => $limit,
            'page'       => $page,
            'total_page' => ceil($total / $limit),
        ];
        $expendWhere   = $where;
        $incomeWhere   = $where;
        $expendWhere[] = ['trade_type', 'in', '1,2,5,14,15,16'];
        $incomeWhere[] = ['trade_type', 'in', '3,4,6,10,11,12,13,17'];
        $totalCount    = [
            'expend'  => Db::name('mall_trade_record_log')->where($expendWhere)->value('SUM(amount)') / 100 ?? 0,
            'income'  => Db::name('mall_trade_record_log')->where($incomeWhere)->value('SUM(amount)') / 100 ?? 0,
            'balance' => Db::name('mall_user')->where([['id', '=', $userId]])->value('balance') / 100 ?? 0,
        ];
        $res           = [
            'total' => $totalCount,
            'page'  => $page,
            'list'  => $tradeRecord,
        ];
        _outPut(200, $res);
    }
}